﻿using M_ApiResult;
using M_Quality.API.Application.Command.RawMaterialQualityCommand;
using M_Quality.Domain;
using M_Quality.Repository;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using OfficeOpenXml;
using M_Quality.Domain.Dto;

namespace M_Quality.API.Controllers
{
    /// <summary>
    /// 原料质检
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RawMaterialQualityController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly MyDbcontract _context;

        public RawMaterialQualityController(IMediator mediator, MyDbcontract context)
        {
            this.mediator = mediator;
            _context = context;
        }

        /// <summary>
        /// 添加原料质检
        /// </summary>
        /// <param name="comm"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddRMQAsync(AddRMQCommand comm)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(comm);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 反填质检项目
        /// </summary>
        /// <param name="Command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Qualityinspection>>> ShowSpectionAsync([FromQuery] BackfillSpectionCommand Command)
        {
            ApiResult<List<Qualityinspection>> result = new ApiResult<List<Qualityinspection>>();
            try
            {
                result.Data = await mediator.Send(Command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 获取原料质检列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<RawMaterialQuality>>> GetListAsync([FromQuery]GetListCommand command)
        {
            ApiResult<List<RawMaterialQuality>> result = new ApiResult<List<RawMaterialQuality>>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 删除原料质检列表信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> RemoveRMQAsync([FromQuery] RemoveRMQCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = await mediator.Send(command);
                return result;
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "删除原料质检列表信息失败";
                throw;
            }
        }

        /// <summary>
        /// 反填显示原料质检信息
        /// </summary>
        /// <param name="Command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<RawMaterialQuality>> ShowRMQAsync([FromQuery] ShowRMQCommand Command)
        {
            ApiResult<RawMaterialQuality> result = new ApiResult<RawMaterialQuality>();
            try
            {
                result.Data = await mediator.Send(Command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code=-1;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 修改原料质检信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdRMQAsync(UpdRMQCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult ImportData([FromBody] ImportRequest model)
        {
            try
            {
                if (model?.Data == null || !model.Data.Any())
                {
                    return BadRequest("无有效数据");
                }

                // 解析 Excel 数据并保存到数据库
                foreach (var row in model.Data)
                {
                    var entity = new RawMaterialQuality
                    {
                        QualityNumber = row[0]?.ToString(), // 根据实际 Excel 列来解析
                        QualityName = row[1]?.ToString(),
                        QualityTime = DateTime.Parse(row[2].ToString())
                    };

                    _context.RawMaterialQuality.Add(entity);
                }

                _context.SaveChanges();

                return Ok("导入成功");
            }
            catch (Exception ex)
            {
                // 捕获异常并返回错误信息
                return StatusCode(500, $"导入失败: {ex.Message}");
            }
        }
    }
}
